﻿using ApiLib.RCL.Components.AutoTable.Models;
using ApiLib.RCL.Models;
using ApiLib.RCL.Utils;
using BootstrapBlazor.Components;

namespace ApiLib.RCL.Components.AutoTable.Attributes;

/// <summary>
/// 列
/// </summary>
public class ColumnAttribute : Attribute
{
    /// <summary>
    /// 名称
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// 宽度，默认auto
    /// </summary>
    public string Width { get; set; } = "auto";

    /// <summary>
    /// 对齐，默认Alignment.Center
    /// </summary>
    public Alignment TextAlign { get; set; } = Alignment.Center;

    /// <summary>
    /// 换行，默认false
    /// </summary>
    public bool TextWrap { get; set; } = false;

    /// <summary>
    /// 显示Tooltip，默认false
    /// </summary>
    public bool TextTooltip { get; set; } = false;
    /// <summary>
    /// 超出部分显示...，默认false
    /// </summary>
    public bool TextEllipsis { get; set; } = false;

    /// <summary>
    /// 显示在列表中，默认true
    /// </summary>
    public bool ShowOnList { get; set; } = true;

    /// <summary>
    /// 新增时显示，默认true
    /// </summary>
    public bool ShowOnAdd { get; set; } = true;

    /// <summary>
    /// 编辑和详情时显示，默认true
    /// </summary>
    public bool ShowOnEdit { get; set; } = true;

    /// <summary>
    /// 搜索时显示，默认false
    /// </summary>
    public bool ShowOnSearch { get; set; } = false;

    /// <summary>
    /// 是否允许为空，默认true
    /// </summary>
    public bool Nullable { get; set; } = true;

    /// <summary>
    /// 排序，越小越靠前
    /// </summary>
    public int Sort { get; set; } = 0;

    /// <summary>
    /// 默认值，用于多选，示例：key1=value1;key2=value2
    /// </summary>
    public string DefaultValue { get; set; }

    /// <summary>
    /// 显示类型，根据FieldType区分：<br/>
    /// Input=>String：text,password,email,number,phone,url。<br/>
    /// Image|File=>String：single,multiple多选。<br/>
    /// Select=>String|Int：int。<br/>
    /// Date|DateRange=>Datetime：datetime,date,year,month。<br/>
    /// </summary>
    public string ShowType { get; set; }

    /// <summary>
    /// 提示信息
    /// </summary>
    public string Placeholder { get; set; }

    /// <summary>
    /// 提交查询表单时的参数，范围时间传递两个，第一个为开始，第二个为结束。不设置时，默认为时间+Start和时间+End
    /// </summary>
    public string[]? ParamNames { get; set; }

    /// <summary>
    /// 当前值
    /// </summary>
    public object Value { get; set; }

    /// <summary>
    /// 时间范围类型，默认None
    /// </summary>
    public DateTimeRangeType DateTimeRangeType { get; set; } = DateTimeRangeType.None;

    /// <summary>
    /// 字段类型
    /// </summary>
    public TableFieldType FieldType { get; set; } = TableFieldType.Input;

    public ColumnAttribute()
    {
    }

    public ColumnAttribute(string name)
    {
        Name = name;
    }

    /// <summary>
    /// 选择项
    /// </summary>
    /// <returns></returns>
    public List<SelectedItem>? GetSelectedItems()
    {
        return string.IsNullOrEmpty(DefaultValue)
            ? null
            : DefaultValue.Split(";").Select(str =>
            {
                var arr = str.Split("=");
                return new SelectedItem(arr[1], arr[0]);
            }).ToList();
    }

    /// <summary>
    /// 获取查询对象
    /// </summary>
    /// <returns></returns>
    /// <exception cref="ArgumentOutOfRangeException"></exception>
    public void InitSearchObject()
    {
        Value = FieldType switch
        {
            TableFieldType.Input or TableFieldType.TextArea or TableFieldType.Radio or TableFieldType.CheckBox
                or TableFieldType.Select or TableFieldType.MultiSelect => "",
            TableFieldType.InputNumber or TableFieldType.InputDouble => 0,
            TableFieldType.Date => ShowType is "datetime" or "date"? DateTimeRangeType.GetValue()! : DateTime.Now,
            TableFieldType.DateRange => DateTimeRangeType.GetValue()!,
            TableFieldType.DateYear => 0,
            TableFieldType.DateMonth => "",
            _ => ""
        };
    }

    public void AddSearchParam(string name, Dictionary<string, object> searchParam)
    {
        if (FieldType is TableFieldType.Date or TableFieldType.DateRange)
        {
            if (Value is DateTimeRangeValue dateRangeValue)
            {
                var start = dateRangeValue.Start.DayStart();
                var end = dateRangeValue.End.DayEnd();
                if (ParamNames is { Length: 2 } && !string.IsNullOrEmpty(ParamNames[0]) &&
                    !string.IsNullOrEmpty(ParamNames[1]))
                {
                    searchParam[ParamNames[0]] = start;
                    searchParam[ParamNames[1]] = end;
                }
                else
                {
                    searchParam[name + "Start"] = start;
                    searchParam[name + "End"] = end;
                }
            }
        }
        else
        {
            var key = ParamNames is { Length: > 0 } && !string.IsNullOrEmpty(ParamNames[0]) ? ParamNames[0] : name;
            searchParam[key] = Value;
        }
    }
    
    /// <summary>
    /// 对齐样式
    /// </summary>
    public string TextAlignClass=>TextAlign switch
    {
        Alignment.Left=>"",
        Alignment.Right=>"end",
        Alignment.Center=>"center",
        _=>"center"
    };
    /// <summary>
    /// 换行样式
    /// </summary>
    public string TextWrapClass=>TextWrap?"is-wrap":"";
    /// <summary>
    /// 超出样式
    /// </summary>
    public string TextEllipsisClass=>TextEllipsis?("is-ellips"+(TextAlign switch
    {
        Alignment.Left=>" text-start",
        Alignment.Right=>" text-end",
        Alignment.Center=>" text-center",
        _=>" text-center"
    })):"";
}